package com.jrx.anytxn.transaction.job.fileRepay.step;

import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.BtBatchStatus;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.base.BtBatchStatusMapper;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2021-03-11 10:30
 **/
@Service
public class DealPayOffListener implements StepExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(DealPayOffListener.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private BtBatchStatusMapper btBatchStatusMapper;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private PaphFileConfig paphFileConfig;


    @Override
    public void beforeStep(StepExecution stepExecution) {

    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        BtBatchStatus btBatchStatus = new BtBatchStatus();
        try {
            btBatchStatus.setId(segmentService.getId(Constant.BT_BATCH_STATUS_ID));//id
        } catch (SegmentException e) {
            logger.error("主键生成错误", e);
        }
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        btBatchStatus.setBatchStatus(Constant.BATCH_SATTUS_2);//状态 2 完成
        btBatchStatus.setBusinessDate(org.getNextProcessingDate());//当前业务日期
        btBatchStatus.setJobName(TransBizConstant.DEAL_PAYOFF_FILE_JOB+paphFileConfig.getFileType());//批处理名称
        btBatchStatus.setTenantId(org.getTenantId());//租户id
        btBatchStatus.setUpdateBy(Constant.DEFAULT_USER);
        btBatchStatus.setRecordVersionNumber(Constant.VERSION_NUMBER);//初始版本号
        btBatchStatusMapper.insertSelective(btBatchStatus);
        return null;
    }
}
